package main;

import java.util.ArrayList;

import javax.xml.crypto.Data;

import Data.FileData;
import Data.HibernateUtil;
import Data.Tag;
import GUI.DropBoxGUI2;
import executor.ExecutorsHash;

/**
 * A general - type of facade, most of the action
 * in the program pass from this class, this class 
 * build in singelton type - there is Instance and 
 * a private constructor
 * 
 * @author Vladimir Sarkisyan & Erez Levy
 *
 */
public class Working {

	
	public static Working Instance = new Working();
	
	private Working(){
		
	}
	
	//exit the program
	public void exitProgram(){
		
	}

	
	/**
	 * the function add the file to the Data base
	 * with a global tag - global@global
	 * 
	 * @param name - the file name
	 */
	public void createFileInDB(String name){
		//create FileData
		FileData file = new FileData(name, "global@global", null, 0);
		
		//add a new file to DB
		HibernateUtil.instance.saveFile(file);		
	}
	
	
	/**
	 * the function delete a file from the Data base
	 * @param name - the file name
	 */
	public void deleteFileInDB(String name){
		HibernateUtil.instance.deleteFile(name);	
	}
	
	/**
	 * the function rename a file name in the Data base
	 * 
	 * @param oldName - the name before the rename
	 * @param newName - the name after the rename
	 */
	public void renameFileInDB(String oldName, String newName){
		HibernateUtil.instance.renameFile(oldName, newName);
	}
	
	/**
	 * the function create a new user tag in the data base
	 * 
	 * @param tag - the name of the tag
	 */
	public void createUserTag(String tag){
		Tag tagObj;		
		tagObj = new Tag(tag, null);
		//for debug 
		System.out.println("in Working - createUserTag");
		//
		
		HibernateUtil.instance.saveTag(tagObj);	
	}
	
	
	/**
	 * find the files that have this userTag - tag that created 
	 * by the user
	 * 
	 * @param userTag - name of the tag, created by the user
	 */
	public void searchFilesByUserTag(String userTag){
		ArrayList<String> fileList;
		//in case it is not all the files
		if(!userTag.equals("all the files")){
			//get list of names file that have this user tag
			fileList = HibernateUtil.instance.getFilesByTag(userTag);	
		}
		//case it is all the files
		else{
			fileList = HibernateUtil.instance.getFilesByTag("global@global");
		}
		
		//Edit the list in the GUI
		DropBoxGUI2.getInstance().editListOfFiles(fileList);
	}
	
	/**
	 * the function get a file and a tag name and create tuple
	 * in the DB 
	 * 
	 * @param fileName -  name of the file
	 * @param tag -  name of the tag
	 */
	public void tagFileByUserTag(String fileName, String tag){
		HibernateUtil.instance.tagFileByUserTag(fileName, tag);
	}	
	
	
	public void searchFiles(){
		
	}	
	
	public void automaticFiles(){
		
	}	
	
	public void userTagFiles(){
		
	}		
}
